package com.swpu.service_xueya.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import com.swpu.service_xueya.entity.XuetangRecord;
import com.swpu.service_xueya.entity.XueyaRecord;
import com.swpu.service_xueya.entity.vo.XuetangChartData;
import com.swpu.service_xueya.entity.vo.XuetangRecordListVo;
import com.swpu.service_xueya.entity.vo.XueyaChartData;
import com.swpu.service_xueya.entity.vo.XueyaRecordListVo;
import com.swpu.service_xueya.service.XuetangRecordService;
import com.swpu.service_xueya.service.XueyaRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-04-21
 */
@RestController
@RequestMapping("/service_xueya/xuetang-record")
public class XuetangRecordController {
        @Autowired
    private XuetangRecordService xuetangRecordService;
    @PostMapping("addXuetangData")
    @Decrypt
    @Encrypt
    public R addXuetangData(@RequestBody ResultEncryption resultEncryption, XuetangRecord xuetangRecord,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        String recordId = xuetangRecordService.addXuetangData(openid, xuetangRecord);
        return R.ok().data("recordId",recordId);
    }
    @GetMapping("getXuetangDataNewest")
    public R getXuetangDataNewest(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        XuetangRecord xuetangRecord = xuetangRecordService.getXuetangDataNewest(openid);
        return R.ok().data("xuetangRecord",xuetangRecord);
    }

    @GetMapping("getXuetangDataById")
    public R getXuetangDataById(HttpServletRequest request,@RequestParam String recordId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        XuetangRecord xuetangRecord = xuetangRecordService.getXuetangDataById(recordId);
        return R.ok().data("xuetangRecord",xuetangRecord);
    }
    @GetMapping("getChartData")
    public R getChartData(HttpServletRequest request,@RequestParam(defaultValue = "2")Integer measureDurationId){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        XuetangChartData chartData = xuetangRecordService.getChartData(openid,measureDurationId);
        return R.ok().data("chartData",chartData);
    }

    @GetMapping("getXuetangDataList")
    public R getXueyaDataList(HttpServletRequest request,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "20") Integer pageSize){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        List<XuetangRecordListVo> xuetangDataList = xuetangRecordService.getXuetangDataList(openid, pageNum, pageSize);
        return R.ok().data("xuetangDataList",xuetangDataList);
    }
    @GetMapping("getRecentMonthXuetangStatus")
    public R getRecentMonthXueyaStatus(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        Map resultMap = xuetangRecordService.getRecentMonthXuetangStatus(openid);
        return R.ok().data(resultMap);
    }
    @GetMapping("getNewestDayXuetangDataList")
    public R getNewestDayXuetangDataList(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        List recordList = xuetangRecordService.getNewestDayXuetangDataList(openid);
        return R.ok().data("recordList",recordList);
    }
}

